package com.heima.freemaker.web;


import com.heima.freemaker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hello")
public class HelloController {
    /**
     * 三种传输方式
     * 1.model  表示模型
     * 2.modelMap  表示map集合
     * 3.modelAndView 表示模型和视图
     */



    @GetMapping
    public String modelTest(Model model) {

        //1.传输数据
        model.addAttribute("name", "霸天虎");
        //2.跳转页面
        return "01-basic";
    }

    @GetMapping("/map")
    public String modelMapTest(ModelMap model) {

        //1.传输数据
        model.put("name", "红蜘蛛");
        //2.跳转页面
        return "01-basic";
    }

    @GetMapping("/mav")
    public ModelAndView mavTest() {

        //1.传输数据
        ModelAndView mav=new ModelAndView();
        mav.addObject("name","法外狂徒张三");
        //2.跳转页面
        mav.setViewName("01-basic");
        return mav;
    }

    @GetMapping("/list")
    public String listTest(Model model) {
        List<Student> list=new ArrayList<>();

        Student student1=new Student();
        student1.setName("张三");
        student1.setAge(23);
        student1.setMoney(300);

        Student student2=new Student();
        student2.setName("赵四");
        student2.setAge(24);
        student2.setMoney(400);

        list.add(student1);
        list.add(student2);
        model.addAttribute("stus",list);

        Map<String,Object> map=new HashMap<>();
        map.put("stu1",student1);
        map.put("stu2",student2);
        model.addAttribute("stuMap",map);

        return "02-list";
    }
}
